package com.mnsq.common.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.mnsq.common.bean.enums.SubjectType;

import java.io.IOException;

/**
 * @author 周俭
 */
public class TypeDeserializer extends JsonDeserializer<Integer> {

    @Override
    public Integer deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        String text = p.getText();
        if (text != null) {
            try {
                return Integer.valueOf(text);
            } catch (NumberFormatException e) {
                return SubjectType.type(text);
            }
        }
        return 0;
    }
}
